package com.adams.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * 
 * File Name : SystemConfig.java
 * 
 * @Description : 系统配置文件类
 * @author 903329
 */
public class SystemConfig {
	private static Logger logger = LoggerFactory.getLogger(SystemConfig.class);

	private static SystemConfig instance = null;

	private static Configuration configuration = null;// 这里采用Configuration以便以后扩展XML等其他配置文件

	private static final String SYSTEM_CONFIG_FILE_NAME = System.getProperty(
			"os.name").toString().startsWith("Windows") ? "win-sysconfig.properties"
			: "linux-sysconfig.properties";// 系统配置文件名称

	private SystemConfig() {
		load();
	}

	public static synchronized SystemConfig getInstance() {
		if (instance == null) {
			synchronized (SystemConfig.class) {
				if (instance == null) {
					instance = new SystemConfig();
				}
			}
		}
		return instance;
	}

	public Configuration getConfiguration() {
		return configuration;
	}

	/**
	 * 
	 * Description : 提供几个获取配置信息的常用方法
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public String getString(String key) {
		return configuration.getString(key);
	}

	/**
	 * 
	 * Description : 提供几个获取配置信息的常用方法
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public int getInt(String key) {
		return configuration.getInt(key);
	}

	/**
	 * 
	 * Description : 提供几个获取配置信息的常用方法
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public String[] getStringArray(String key) {
		return configuration.getStringArray(key);

	}

	/**
	 * 
	 * Description : 提供几个获取配置信息的常用方法
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public Boolean getBoolean(String key) {
		return configuration.getBoolean(key);

	}

	/**
	 * 
	 * Description : 提供几个获取配置信息的常用方法
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public Object getProperty(String key) {
		return configuration.getProperty(key);
	}

	/**
	 * 
	 * Description : 加载配置文件信息
	 * 
	 * 
	 */
	private void load() {
		String filePath = getAbsApplicationPath() + "config"
				+ System.getProperty("file.separator")
				+ SYSTEM_CONFIG_FILE_NAME;

		// 读配置文件
		try {
			configuration = new PropertiesConfiguration(filePath);
			if (configuration == null) {
				logger.error("系统配置文件" + SYSTEM_CONFIG_FILE_NAME + "不存在!");
				throw new IllegalArgumentException("系统配置文件"
						+ SYSTEM_CONFIG_FILE_NAME + "不存在!");
			}
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			logger.error("读系统配置文件" + SYSTEM_CONFIG_FILE_NAME + "出现错误!");
			throw new IllegalArgumentException("读系统配置文件"
					+ SYSTEM_CONFIG_FILE_NAME + "出现错误!");
		}

	}

	/**
	 * 设定参数
	 * 
	 * @param key
	 * @param value
	 */
	public void setString(String key, String value) {
		configuration.setProperty(key, value);
	}

	/**
	 * @Description: 读取定时任务参数属性
	 */
	public static String getScheduleTimeProperty(String key) {
		InputStream fis = null;
		String filePath = getAbsApplicationPath() + "config"
				+ System.getProperty("file.separator") + "scheduler.properties";
		Properties properties = new Properties();

		try {
			fis = new FileInputStream(new File(filePath));
			properties.load(fis);
			return properties.getProperty(key);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	/**
	 * @Description: 保存定时任务参数属性
	 */
	public static boolean storeScheduleTime(String key, String value) {

		String filePath = getAbsApplicationPath() + "config"
				+ System.getProperty("file.separator") + "scheduler.properties";
		InputStream fis = null;
		Properties properties = new Properties();
		OutputStream os = null;

		try {
			fis = new FileInputStream(new File(filePath));
			properties.load(fis);

			for (Enumeration e = properties.propertyNames(); e
					.hasMoreElements();) {
				String pro_key = (String) e.nextElement(); // 遍历所有元素
				if (pro_key.equals(key)) {
					properties.setProperty(key, value);
					/** 如果键相同则覆盖 */
				} else {
					properties.setProperty(pro_key, properties
							.getProperty(pro_key));
				}
			}

			os = new FileOutputStream(new File(filePath));

			properties.store(os, "schedule time config ");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			if (fis != null)
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (os != null)
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

		return true;
	}

	public static String getAbsApplicationPath() {
		String path = SystemConfig.class.getClassLoader().getResource("")
				.getPath();
		try {
			path = URLDecoder.decode(path, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		path = path
				.replaceAll("/", "\\" + System.getProperty("file.separator"));
		if (System.getProperty("file.separator").equals("\\"))// windows
			path = path.substring(1);

		return path;
	}

}
